using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerDataManger : MonoBehaviour
{
    static public PlayerDataManger instance;
    public PlayerData playerData;
    public bool inBossArea =false;
    private PlayerController playerController;
    private LayerMask groundLayer;
    
    private void Awake()
    {
        if(instance != null )
            Destroy(this);
        instance = this;
    }

    private void Start()
    {
        playerController = GameObject.FindGameObjectWithTag(Tags.Player).GetComponent<PlayerController>();
        playerController.StateData.CurHP = playerData.currentHP;
        playerController.gameObject.transform.position = playerData.position;
        groundLayer |= 1 << (LayerMask.NameToLayer(LayerStrings.Ground));
    }

    void Update()
    {
        playerController = GameObject.FindGameObjectWithTag(Tags.Player).GetComponent<PlayerController>();
        RaycastHit2D hit = Physics2D.Raycast(playerController.gameObject.transform.position, Vector2.down, 2, groundLayer);
        if(transform.parent == null&& hit.collider !=null&&!inBossArea)
            playerData.position = playerController.gameObject.transform.position;
        int curHP = playerController.StateData.CurHP;
        int maxHP = playerController.ConfigData.MaxHP;
        if (curHP > maxHP)
            playerData.currentHP = maxHP;
        else
            playerData.currentHP = curHP;
    }

    public void setRevivalPoint(Vector3 revivalPosition)
    {
        playerData.revivalPosition = revivalPosition;
    }

    public void revival()
    {
        playerData.position =playerData.revivalPosition;
        playerData.currentHP = playerController.ConfigData.MaxHP;
        playerController.StateData.CurHP = playerData.currentHP;
    }
}
